﻿using System;
using System.Collections.Generic;
using System.Data;
using WorkFlow.DBProxy;

namespace WorkFlow.Engine.NSPermission
{
    public class WfUser
    {
        #region fields

        public readonly List<int> _orgsCollection = new List<int>(); //用来存放用户所属单位
        public readonly List<int> _rolesCollection = new List<int>(); //用来存放用户所属角色

        public int ID { get; private set; }

        public string Name { get; private set; }

        public bool IsCreated { get; private set; }

        public DateTime LastModifiedTime { get; set; }

        #endregion fields

        #region constructor

        public WfUser(int userID)
        {
            DataTable dt =
                DB.MapToDataTable(string.Format(
                    @"  select F_ID,F_NAME from t_users WHERE f_id={0}", userID));
            if (dt == null || dt.Rows.Count == 0)
            {
                IsCreated = false;
                return;
            }
            ID = Convert.ToInt32(dt.Rows[0]["F_ID"]);
            Name = dt.Rows[0]["F_NAME"].ToString();

            //---填充所属角色集合
            dt =
                DB.MapToDataTable(string.Format(@"select f_role_Id as ROLEID from t_role_user WHERE  f_user_id={0}",
                    userID));
            if (dt != null && dt.Rows.Count > 0)
            {
                int rows = dt.Rows.Count;
                for (int i = 0; i < rows; i++)
                {
                    _rolesCollection.Add(Convert.ToInt32(dt.Rows[i]["ROLEID"]));
                }
            }

            // ----填充所属单位集合
            dt =
                DB.MapToDataTable(string.Format(@"select f_org_id as ORGID from t_org_users where f_user_id={0}",
                    userID));
            if (dt != null && dt.Rows.Count > 0)
            {
                int rows = dt.Rows.Count;
                for (int i = 0; i < rows; i++)
                {
                    _orgsCollection.Add(Convert.ToInt32(dt.Rows[i]["ORGID"]));
                }
            }

            LastModifiedTime = DateTime.Now;
            IsCreated = true;
        }

        #endregion constructor

        #region self defined method

        //用户是否输入该角色
        public bool IsBelongToRole(int roleID)
        {
            return _rolesCollection.Contains(roleID);
        }

        //用户是否输入该组织
        public bool IsBelongToOrg(int orgID)
        {
            WfOrganization org = WfPermissionController.CreatePermissionController().GetOrgaization(orgID);
            if (!org.IsCreated) return false;
            //如果org为抽象org 那么检查该用户是否属于该org的具体org
            if (org.IsAbstractOrg == WfFieldCode.DbTrue)
            {
                int counts = _orgsCollection.Count;
                List<WfOrgStruct> list = org._specificOrgsCollection;
                int spe = list.Count;
                for (int i = 0; i < counts; i++)
                {
                    for (int r = 0; r < spe; r++)
                    {
                        if (_orgsCollection[i] == list[r]._id) return true;
                    } //end inner for loop
                } //end outer for loop
            } // end if is abstract

            //如果该组织不受抽象的话 就直接检查是否包含就可以了
            return _orgsCollection.Contains(orgID);
        }

        /// <summary>
        ///     设置运行库活动的用户
        /// </summary>
        /// <param name="newAccessors">int{组织id,角色id,用户id} 没有id设置为 0</param>
        /// <returns></returns>
        // ReSharper disable once InconsistentNaming
        public bool setRunActivityAccessors(List<int[]> newAccessors)
        {
            return false;
        }

        #endregion self defined method
    }
}